<?php
 /**
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   *
   * ----------------------------------------------------------------------
   * NimWiki - designer centric wiki
   *
   * @author Nicolas Myers <myers.nicolas@gmail.com>
   * @copyright 2007 Nicolas Myers
   * @version 0.9.0
   * @link
   * ----------------------------------------------------------------------
   * PLUGINS.PHP
   *
   * collection of functions/plugins used in the wiki syntax
   * ----------------------------------------------------------------------
   */
   
   /**
    * Returns found pages according to POST values
    * @return  string  xhtml list produced
    */
    function plugin_findpage()
    {
        global $cfg;
        $content = "";
        $wiki_get = $cfg['wiki_get'];
        $privatePages = array("AllPages", "RecentChanges", "FindPage");
        $resultList = array();
        $search = explode(" ", " " . urldecode(@$_GET["PageContent"]));
        // list all existant pages
        function toshort($var) {
            return(strlen($var) > 2);
        }
        $handle = opendir($cfg['data_dir'] . $cfg['lang'] . "/");
        while ($newfile = basename(readdir($handle), ".dat")) {
            if ((strpos($newfile, '.') === false) and (!in_array($newfile, $privatePages))) {
                //$newfile
                $score = 0;
                $filect = file_get_contents(get_filename($newfile));
                foreach($search as $searchWord)if($searchWord != "")$score += substr_count($filect, $searchWord);
                if($score != 0)$resultList[$newfile] = $score;
            }
        }
        arsort($resultList);
        $content .= "<h1>Résultat de la recherche : " . urldecode(@$_GET["PageContent"]) . "</h1>";
        $content .= "<ul>";
        foreach($resultList as $page => $score)$content .= "<li><a href=\"index.php?$wiki_get=$page\">" . unNormalizeStr($page) . "</a> ($score)</li>";
        $content .= "</ul>";
        if(empty($resultList))$content .= "Pas de résultat";
        return $content;
    }
    
   /**
    * Returns a list of pages recently changed in the wiki
    * ordered by date
    * @return  string  xhtml list produced
    * @todo fix this!!
    */
    function plugin_recentchanges()
    {
        global $lang;
        $wiki_get = WIKI_GET;
        
        $timezone = TIMEZONE;
        
        $content = "";
        $allpages = array();
        $handle = opendir(DATA_DIR . "/$lang");
        $privatePages = array("AllPages", "RecentChanges", "FindPage");
        while ($newfile = basename(readdir($handle), ".dat")) {
            if ((strpos($newfile, '.') === false) and (!in_array($newfile, $privatePages))) {
                $allpages[] = $newfile;
            }
        }
        $max_pages = max(30, count($allpages) / 3);
        $counter = 0;
        $date = mktime(12, 0, 0, date('m'), date('d'));
        $day = 0;
        while ($counter < $max_pages && $day < 40) {
            $today = array();
            foreach ($allpages as $page) {
                $filetime = filemtime(DATA_DIR . $lang . "/$page.dat");
                if((($filetime + 43199) > $date) && (($filetime - 43200) < $date))$today[] = $page;
            }
            if (count($today) > 0) {
                $content .= "<h3>" . date("d.m.Y", $date) . "</h3>\n<ul>\n";
                foreach($today as $page)$content .= "<li><a href=\"index.php?$wiki_get=$page\">" . unNormalizeStr($page) . "</a> (" . date("G:i", filemtime(DATA_DIR . "$lang/$page.dat") + (3600 * $timezone)) . ")</li>";
                $content .= "</ul>\n";
            }
            $counter += count($today);
            $date -= 86400;
            $day += 1;
        }
        $content .= "</ul>\n";
        return $content;
    }

   /**
    * Returns an alphabeticaly ordered list of all the pages in the wiki
    * @return  string  xhtml list produced
    */
    function plugin_allpages()
    {
        global $cfg;
        $wiki_get = $cfg['wiki_get'];
        $privatePages = array("AllPages", "RecentChanges", "FindPage");
        
        // list all existant pages
        $content = "<ul>";
        $handle = opendir($cfg['data_dir'] . $cfg['lang'] . "/");
        while ($newfile = basename(readdir($handle), ".dat")) {
            if ((strpos($newfile, '.') === false) and (!in_array($newfile, $privatePages))) {
                $allpages[] = $newfile;
            }
        }
        sort($allpages);
        foreach ($allpages as $page) {
            $page = str_replace("#", "/", $page);
            $content .= "<li><a href=\"index.php?$wiki_get=$page\">" . unNormalizeStr($page) . "</a></li>\n";
        }
        
        $content .= "</ul>\n";
        return $content;
    }

   /**
    * Returns an alphabeticaly ordered list of all the pages in the wiki
    * @return  string  xhtml list produced
    */
    function plugin_allpages2()
    {
        global $cfg;
        $wiki_get = $cfg['wiki_get'];
        $privatePages = array("AllPages", "RecentChanges", "FindPage");
        
        // list all existant pages
        $content = "<ul>";
        $handle = opendir($cfg['data_dir'] . $cfg['lang'] . "/");
        while ($newfile = basename(readdir($handle), ".dat")) {
            if ((strpos($newfile, '.') === false) and (!in_array($newfile, $privatePages))) {
                $allpages[] = $newfile;
            }
        }
        sort($allpages);
        foreach ($allpages as $page) {
            $page = str_replace("#", "/", $page);
            $content .= "<li><a href=\"index.php?$wiki_get=$page\">" . unNormalizeStr($page) . "</a>";
            
            $content .= "&nbsp;&nbsp;&nbsp;";
            $content .= isAuthorized("read", $page) ? "R " : "";
            $content .= isAuthorized("edit", $page) ? "E " : "";
            $content .= isAuthorized("create", $page) ? "C " : "";
            $content .= isAuthorized("delete", $page) ? "D " : "";
            
            $content .= "</li>\n";
        }
        
        $content .= "</ul>\n";
        return $content;
    }

    
   /**
    * Returns a hierarchical ordered list of all the pages in the wiki
    * (uses categories)
    * @return  string  xhtml list produced
    */
    function plugin_sitemap()
    {
        global $cfg;
        $wiki_get = $cfg['wiki_get'];
        $privatePages = array("AllPages", "RecentChanges", "FindPage");
        
        // list all existant pages
        $allpages=array();
        $handle = opendir($cfg['data_dir'] . $cfg['lang'] . "/");
        while ($newfile = basename(readdir($handle), ".dat")) {
            if ((strpos($newfile, '.') === false) and (!in_array($newfile, $privatePages))) {
                $tmpname="root#".$newfile;
                $where=&$allpages;
                while (strpos($tmpname,'#')!==false) {
                    $cat=explode('#',$tmpname);
                    $catb=array_shift($cat);
                    $tmpname=(count($cat)==1) ?  $cat[0] : implode("#",$cat);
                    if (!isset($where["$catb"])) $where["$catb"]=array();
                    $where=&$where["$catb"];
                }
                $where[]=$newfile;
            }
        }
        
        function makeList($arr) {
            ksort($arr,SORT_STRING);
            $content='<ul>';
            foreach ($arr as $key => $item) {
                if (is_array($item)) {
                    $content.="<li>".$key."<br/>".makeList($item)."</li>";
                } else {
                    $item = str_replace("#", "/", $item);
                    $name = array_pop(explode("/","/".$item));
                    $content .= "<li><a href=\"".makeUrl($item)."\" >" . unNormalizeStr($name) . "</a></li>\n";
                }
            }
            $content .='</ul>';
            return $content;
        }
        return makeList($allpages['root']);
    }
    
   /**
    * Includes a page in another one (possible problem with loops!!)
    * @param  array   $pagename  pagename to include
    * @param  string  xhtml of the included page
    */
    function plugin_include($pagename)
    {
        return wiki2html($pagename);
    }
    
   /**
    * Makes a javascript slideshow
    * @param  array   $pagename  pagename to include
    * @param  string  xhtml of the included page
    */
    function plugin_slideshow($args)
    {
        $imglist=explode(",",",".$args);
        array_shift($imglist);
        list($width, $height) = getimagesize("media/".$imglist[0]);
        $html  ='<div class="slideshow" style="width:'.$width.'px;height:'.$height.'px;overflow:hidden;" >'."\n";
        foreach ($imglist as $img)
            $html .= "<img src=\"media/$img\" width=\"$width\" />\n";
        $html .= '</div>';
        return $html;
    }
    
?>
